package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.mapper.AgentRebateRuleInfoMapper;
import com.weixing.mall.provider.model.domain.AgentRebateRule;
import com.weixing.mall.provider.model.dto.AgentRebateRuleDto;
import com.weixing.mall.provider.model.query.AgentRebateRuleQuery;
import com.weixing.mall.provider.model.vo.AgentRebateRuleVo;
import com.weixing.mall.provider.model.vo.RebateRuleVo;
import com.weixing.mall.provider.model.vo.RuleInfoVo;
import com.weixing.mall.provider.service.IAgentRebateRuleInfoService;
import com.weixing.mall.provider.service.IAgentRebateRuleService;
import com.weixing.mall.provider.util.AgentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 代理商返利规则API
 *
 * @author  Small
 * @date  2019/10/17 11:03
 * @since  1.0
 **/
@RestController
@RequestMapping("agent/rebateRule")
public class AgentRebateRuleController extends BaseController {
    @Autowired
    private IAgentRebateRuleService agentRebateRuleService;
    @Autowired
    private AgentRebateRuleInfoMapper agentRebateRuleInfoMapper;
    @Autowired
    private IAgentRebateRuleInfoService agentRebateRuleInfoService;

    /**
     * 加载返利规则列表
     *
     * @param query   查询参数
     * @return        返利规则列表
     */
    @GetMapping("")
    public Result<IPage<AgentRebateRuleVo>> list(AgentRebateRuleQuery query) {
        IPage<AgentRebateRuleVo> pageInfo = agentRebateRuleService.findList(initMpPage(), query);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 保存返利规则
     *
     * @param dto   参数
     * @return      结果
     */
    @PostMapping("save")
    @NoRepeatSubmit
    @SysLog(type = OperateEnum.S, desp = "编辑代理商返利规则")
    public Result save(@RequestBody AgentRebateRuleDto dto) {
        if (null == dto.getId()){
            String ruleName = dto.getRuleName();
            AgentRebateRule agentRebateRule = agentRebateRuleService.selectByName(ruleName);
            if (null != agentRebateRule){
                return ResultUtil.error("已存在同名规则");
            }
        }
        agentRebateRuleService.save(dto);
        return ResultUtil.success();
    }

    /**
     * 删除返利规则
     * @param id   id
     * @return     结果
     */
    @PostMapping("/del/{id}")
    @SysLog(type = OperateEnum.S, desp = "删除代理商返利规则")
    public Result del(@PathVariable(value = "id") long id) {
        agentRebateRuleService.deleteById(id);
        return ResultUtil.success();
    }

    /**
     * 根据名称模糊查询返利规则
     * @param name     规则名称
     * @return         返利规则列表
     */
    @PostMapping("/getByName")
    public Result<List<RebateRuleVo>> getByName(String name) {
        List<RebateRuleVo> ruleVos = agentRebateRuleService.getByName(name);
        if (CollUtil.isNotEmpty(ruleVos)) {
            for (RebateRuleVo vo : ruleVos) {
                List<RuleInfoVo> infoVos = agentRebateRuleInfoService.selectByRuleId(vo.getId());
                vo.setRuleInfos(infoVos);
            }
        }
        return ResultUtil.success(ruleVos);
    }

    /**
     * 根据id查询返利规则详情
     * @param id    id
     * @return
     */
    @GetMapping("/{id}")
    public Result<RebateRuleVo> detail(@PathVariable Long id){
        AgentRebateRule rebateRule  = agentRebateRuleService.getById(id);
        List<RuleInfoVo> ruleInfoVos = agentRebateRuleInfoService.selectByRuleId(rebateRule.getId());
        RebateRuleVo rebateRuleVo = new RebateRuleVo();
        rebateRuleVo.setId(rebateRule.getId());
        rebateRuleVo.setRuleName(rebateRule.getRuleName());
        rebateRuleVo.setIsEnable(rebateRule.getIsEnable());
        rebateRuleVo.setOrderBy(rebateRule.getOrderBy());
        rebateRuleVo.setRemark(rebateRule.getRemark());
        rebateRuleVo.setTreeDept(rebateRule.getTreeDept());
        rebateRuleVo.setRuleInfos(ruleInfoVos);
        return  ResultUtil.success(rebateRuleVo);
    }

    /**
     * 加载返利规则详情
     * @param level        等级
     * @param ruleId       规则id
     * @return             规则详情
     */
    @GetMapping("level")
    public Result<List<RuleInfoVo>> ruleInfoList(
            Integer level,
            Long ruleId) {
        if (null == level) {
            return ResultUtil.error("等级不能为空");
        }
        int intValue = level.intValue();
        if (intValue < 1 || intValue > 6) {
            return ResultUtil.error("代理商等级取值范围为1至6");
        }

        if (null != ruleId) {
            List<RuleInfoVo> voList = agentRebateRuleInfoMapper.selectByRuleIdAndTreeDeep(ruleId, level);
            if (null != voList && !voList.isEmpty()) {
                return ResultUtil.success(voList);
            }
        }


        String[] agentArry = new String[]{"经销商", "一级代理", "二级代理", "三级代理", "四级代理", "五级代理"};
        String[] newArry = new String[level];
        for (int i = 0; i < level; i++) {
            newArry[i] = agentArry[i];
        }
        List mnList = AgentUtil.mn(newArry, level);
        ArrayList<RuleInfoVo> fmtForPage = AgentUtil.fmtForPage(newArry, mnList);
        return ResultUtil.success(fmtForPage);
    }


}
